package com.happycat.controller.home;


import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.happycat.base.PageParams;
import com.happycat.base.R;
import com.happycat.home.dto.CatTeamCategoryItemQueryDto;
import com.happycat.home.vo.CatTeamCategoryItemVo;
import com.happycat.home.vo.CatTeamCategoryVo;
import com.happycat.mobile.model.CatTeamCategory;
import com.happycat.mobile.model.CatTeamCategoryItem;
import com.happycat.mobile.service.CatTeamCategoryItemService;
import com.happycat.mobile.service.CatTeamCategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author liaoyong
 * @since 2020-08-01
 */
@RestController
@RequestMapping("/catTeamCategoryItem")
@Api(tags = "API-大神技能品类", description = "大神技能品类接口")
public class CatTeamCategoryItemController {
    @Autowired
	private CatTeamCategoryItemService catTeamCategoryItemService;

    @Autowired
    private CatTeamCategoryService catTeamCategoryService;
	
//	@ApiOperation(value = "大神技能品类详情")
//    @RequestMapping(method = RequestMethod.POST, value = "/detail")
//	public ResponseModel<CatTeamCategoryItem> detail(@RequestBody CatTeamCategoryItem catTeamCategoryItem){
//		return catTeamCategoryItemService.selectOne(catTeamCategoryItem);
//	}


    @GetMapping("/allTeamCategoryList")
    @ApiOperation(value = "技能品类分组列表")
    public R<List<CatTeamCategory>> allTeamCategoryList(){
        return R.success(catTeamCategoryService.list());
    }
	
	@RequestMapping(method = RequestMethod.POST, value = "/page")
	@ApiOperation(value = "大神技能品类分页列表")
	public R<Page<CatTeamCategoryItem>> page(@RequestBody PageParams<CatTeamCategoryItemQueryDto> pageParams){
        Page<CatTeamCategoryItem> page = pageParams.buildSimplePage();
        page.setOrders(Arrays.asList(OrderItem.asc("sort_id")));

        boolean isQueryByCategory = pageParams.getModel()!=null && pageParams.getModel().getTeamCategoryId()!=null;
		return R.result(200, catTeamCategoryItemService.page(page, Wrappers.<CatTeamCategoryItem>lambdaQuery()
                .eq(isQueryByCategory, CatTeamCategoryItem::getTeamCategoryId,
                        isQueryByCategory?pageParams.getModel().getTeamCategoryId():null)), "success");
	}

	@RequestMapping(method = RequestMethod.POST, value = "/allCategoryList")
	@ApiOperation(value = "大神技能品类--更多列表", notes = "查询所有品类分组及其所有品类")
	public R<List<CatTeamCategoryVo>> allCategoryList(){
        List<CatTeamCategory> categoryList = catTeamCategoryService.list();
        if(categoryList==null || categoryList.isEmpty()){
            return R.success(Arrays.asList());
        }
        List<CatTeamCategoryVo> voList = new ArrayList<>(categoryList.size());
        categoryList.forEach(catTeamCategory -> {
            //复制品类分组
            CatTeamCategoryVo vo = BeanUtil.copyProperties(catTeamCategory, CatTeamCategoryVo.class);
            voList.add(vo);
            //查询品类列表
            List<CatTeamCategoryItem> teamCategoryItems = catTeamCategoryItemService.list(Wrappers.<CatTeamCategoryItem>lambdaQuery()
                    .eq(CatTeamCategoryItem::getTeamCategoryId, catTeamCategory.getId())
                    .orderByAsc(CatTeamCategoryItem::getSortId));
            if(teamCategoryItems!=null && !teamCategoryItems.isEmpty()){
                List<CatTeamCategoryItemVo> itemVos = new ArrayList<>(teamCategoryItems.size());
                teamCategoryItems.forEach(teamCategoryItem -> {
                    CatTeamCategoryItemVo itemVo = BeanUtil.copyProperties(teamCategoryItem, CatTeamCategoryItemVo.class);
                    itemVos.add(itemVo);
                });
                vo.setCatTeamCategoryItemVos(itemVos);
            }
        });

        return R.success(voList);
	}

	//	@RequestMapping(method = RequestMethod.POST, value = "/add")
//	@ApiOperation(value = "大神技能品类新增")
//	public ResponseHeaderModel add(@RequestBody CatTeamCategoryItem catTeamCategoryItem){
//
//	   return catTeamCategoryItemService.add(catTeamCategoryItem);
//	}
//
//	@ApiOperation(value = "大神技能品类编辑")
//	@RequestMapping(method = RequestMethod.POST, value = "/modify")
//	public ResponseHeaderModel modify(@RequestBody CatTeamCategoryItem catTeamCategoryItem){
//
//	   return catTeamCategoryItemService.modify(catTeamCategoryItem);
//	}
	
}

